package com.bosch.in.jfree.chart;

import java.awt.Color;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
import org.jfree.chart.labels.CategoryToolTipGenerator;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.util.ShapeUtilities;

import com.bosch.in.rtp1.ept.model.dto.LineChartPointFactor;


public class LineChart extends AbstractDualAxisDoubleBarLineChart {
	//Fixed line color
	private static String[] LINE_STACK_COLORS = {"#00B0F0" , "#0069BD" , "#001558" , "#CCCC00" ,"#99CC00" , "#5C9200" };
	private static String[] BAR_STACK_COLORS = {"#FFFF00","#00FF00"};
	
	//Chart points 
	private Collection<LineChartPointFactor> frFactorColl = null;
	//Tooltips list
	private List<String> toolTips =new LinkedList<String>();
	//define if user can click on the tooltip of this chart or not
	private boolean isClickableTooltip;
	
	/**
	 * 
	 * @param frFactors
	 * @param title
	 */
	public LineChart(List<LineChartPointFactor> frFactors, String... title){
		super(title);
		frFactorColl = frFactors;
		this.setClickableTooltip(false);
	}
	
	@Override
	public CategoryDataset createDatasetLeftAxis() {
		return null;
	}
	
	/**
	 * Set the data set for line chart on left y-axis
	 */
	public CategoryDataset createDatasetLeftAxis1() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		int lineIndex = -1;
		int nextPosition = 0;
		String currentLine = "";
		if(frFactorColl!=null && frFactorColl.size()>0){
			for (Iterator<LineChartPointFactor> iterator = frFactorColl.iterator(); iterator.hasNext();) {
				LineChartPointFactor frFactor = (LineChartPointFactor) iterator.next();
				dataset.addValue(frFactor.getValue(), frFactor.getType().toString(), frFactor.getMonth());
				
				if (!frFactor.getType().toString().equalsIgnoreCase(currentLine))
				{
					//change type of line happened
					currentLine = frFactor.getType().toString();
					++lineIndex;
					nextPosition = lineIndex;
				}				
				else
				{
					nextPosition += lineIndex +1 ;
				}
				if (frFactor.getValue() != null)
				{
					toolTips.add(nextPosition, frFactor.getComment());
				}
				else
				{
					toolTips.add(nextPosition, null);
				}
			}
		}
		return dataset;
	}
	
	/**
	 * 
	 */
	public List<String> getRemovableLegends() {
		List<String> legendList=null;
		//if(hardwaresForm.getStage()==1){
		legendList = new ArrayList<String>();
		legendList.add("Left");
		//}
		return legendList;
	}
	
	@Override
	public Color[] getLeftAxisColors1() {
		Color[] colors = new Color[LINE_STACK_COLORS.length];
		// paints array on the basis of number of categories
		for (int i = 0; i < LINE_STACK_COLORS.length; i++) {
			colors[i] = Color.decode(LINE_STACK_COLORS[i]);
		}
		return colors;
	}
	
	@Override
	public Color[] getLeftAxisColors() {
		Color[] colors = new Color[BAR_STACK_COLORS.length];
		// paints array on the basis of number of categories
		for (int i = 0; i < BAR_STACK_COLORS.length; i++) {
			colors[i] = Color.decode(BAR_STACK_COLORS[i]);
		}
		return colors;
	}
	
	@Override
	public boolean writeImageMap() {
		return false;
	}
	
	@Override
	public CategoryToolTipGenerator getToolTipGeneratorLeftYAxis1() {
		return getToolTipGeneratorLeftYAxis();
	}
	@Override
	public StandardCategoryToolTipGenerator getToolTipGeneratorLeftYAxis() {
		return new StandardCategoryToolTipGenerator();
	}
	
	@Override
	public ToolTipTagFragmentGenerator getToolTipTagFragmentGenerator() {
		return new DefaultToolTipTagFragmentGenerator(toolTips,isClickableTooltip);
	};

	@Override
	public Shape[] getLeftAxisShapes1() {
		Shape shape = ShapeUtilities.createDiagonalCross(1.5f,1.5f);
		return new Shape[] { shape,
				ShapeUtilities.createDiagonalCross(0.0f, 0.0f)};
	}
	
	public void setClickableTooltip(boolean isClickableTooltip) {
		this.isClickableTooltip = isClickableTooltip;
	}

	public boolean isClickableTooltip() {
		return isClickableTooltip;
	}
}
